using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace SPSolutions.Samples.PickerApp.WebControls
{
    /// <summary>
    /// Represents a web selector control
    /// </summary>
    public class CitySelector : ContextSelectorEntityEditorAdapter<City>
    {
        /// <summary>
        /// Get the picker dialog type
        /// </summary>
        public override Type PickerDialogType
        {
            get { return typeof(CityPickerDialog); }
        }

        /// <summary>
        /// Current name of selected item
        /// </summary>
        public override string CurrentName
        {
            get 
            {
                if (base.CurrentItem == null)
                {
                    return string.Empty;
                }
                return base.CurrentId;
            }
        }

        /// <summary>
        /// Default id if not current id is provided
        /// </summary>
        public string DefaultId
        {
            get 
            {
                if (ViewState["DefaultId"] == null)
                    return string.Empty;


                return (string)ViewState["DefaultId"]; 
            }
            set { ViewState["DefaultId"] = value; }
        }

        /// <summary>
        /// Key
        /// </summary>
        protected override string Key
        {
            get { return "City"; }
        }

        /// <summary>
        /// Navigate url
        /// </summary>
        //protected override string NavigateUrl
        //{
        //    get 
        //    {
        //        return String.Format(
        //            "{0}&{1}={2}&MultiSelect=False", 
        //            base.NavigateUrl,
        //            ConfigRes.QueryString_SiteCollectionUrl,
        //            HttpUtility.UrlEncode(SiteCollectionUrl));
        //    }
        //}


        /// <summary>
        /// Get type name for the selector
        /// </summary>
        protected override string TypeName
        {
            get { return "City"; }
        }

        /// <summary>
        /// Default selection id
        /// </summary>
        /// <returns></returns>
        protected override string DefaultSelectionId()
        {
            // Return the default id
            if (!string.IsNullOrEmpty(DefaultId))
            {
                return DefaultId;
            }

            // No default
            return null;
        }

        //public void Reload()
        //{
        //    if (!Page.IsPostBack && ((CurrentItem == null)))
        //    {
        //        string idFromQueryString = IdFromQueryString;
        //        if (idFromQueryString == null)
        //        {
        //            if (string.IsNullOrEmpty(ClearSelectionText))
        //            {
        //                CurrentId = CookieCurrentId;
        //                if ((CurrentItem == null))
        //                {
        //                    CurrentId = DefaultSelectionId();
        //                }
        //            }
        //        }
        //        else
        //        {
        //            CurrentId = idFromQueryString;
        //        }
        //    }

        //}

        /// <summary>
        /// Get the default selection id
        /// </summary>
        public string DefaultSelectionIdPublic()
        {
            return DefaultSelectionId();
        }

        /// <summary>
        /// Get item
        /// </summary>
        /// <returns></returns>
        protected override City GetItem()
        {
            // No site collection provided
            if (CityManager.AllCities == null || CityManager.AllCities.Count == 0)
            {
                return null;
            }

            // Get the current city
            return CityManager.AllCities.GetCityNoThrow(base.CurrentId);
        }
    }
}
